package org.practice.action;
import java.io.Serializable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.practice.model.User;
import org.practice.util.Constant;

/**
 * 基础Action
 * @author jack
 * @since 2.0.0
 */
public class BaseAction extends com.opensymphony.xwork2.ActionSupport implements Serializable{
	/**
     * 用于模拟action测试的request 正常情况下为空只有在debug模式才会有值
     */
    private HttpServletRequest debugRequest;
	public static final String SERVER_URI_PREFIX = "http://";
	public static final String PORT_SEPARATOR = ":";
	public static final String FAIL = "fail";

	/**
	 * 获得HttpServletRequest
	 * 
	 */
	public HttpServletRequest getRequest() {
        if (debugRequest==null) {
            return ServletActionContext.getRequest();
        }else
            return debugRequest;
    }

	/**
	 * 获得HttpSession
	 * @return
	 */
	public HttpSession getSession() {
		HttpServletRequest req = getRequest();
		if (req == null)
			return null;
		return req.getSession();
	}

	/**
	 * 封装HttpServletRequest的方法
	 * @param name Hash表的key
	 * @param obj Hash表的value
	 */
	protected void setRequestAttribute(String name, Object obj) {
		HttpServletRequest req = getRequest();
		if (req != null)
			req.setAttribute(name, obj);
	}

	/**
	 * 封装HttpServletRequest的方法
	 * @param name 通过Hash的key
	 * @return Object 返回value
	 */
	protected Object getRequestAttribute(String name) {
		HttpServletRequest req = getRequest();
		if (req == null)
			return null;
		return req.getAttribute(name);
	}

	/**
	 * 封装HttpSession的方法
	 * @param name Hash表的key
	 * @param obj Hash表的value
	 */
	protected void setSessionAttribute(String name, Object obj) {
		HttpSession session = getSession();
		if (session != null)
			session.setAttribute(name, obj);
	}

	/**
	 * 封装HttpSession的方法
	 * @param name 通过Hash的key
	 * @return Object 返回value
	 */
	protected Object getSessionAttribute(String name) {
		HttpSession session = getSession();
		if (session == null)
			return null;
		return session.getAttribute(name);
	}

	/**
	 * 移除HttpSession scope 缓存的对象
	 * 
	 * @param name Hash表的key
	 */
	protected void removeSessionAttribute(String name) {
		HttpSession session = getSession();
		if (session != null)
			session.removeAttribute(name);
	}

	/**
	 * 移除HttpRequest scope 缓存的对象
	 * @param name Hash表的key
	 */
	protected void removeRequestAttribute(String name) {
		HttpServletRequest req = getRequest();
		if (req != null)
			req.removeAttribute(name);
	}

	/**
	 * 装HttpServletResponse对象
	 * @return 返回HttpServletResponse
	 */
	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public String execute() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 获得登陆用户
	 * @return
	 * @throws Exception
	 */
	public User getUser() throws Exception{
		return (User)getSession().getAttribute(Constant.SESSION_KEY_USER);
	}
	
	/**
	 * 获得应用的url
	 * @return
	 */
	protected String getServerPath() {
		if (getRequest().getServerPort() == 80)
			return SERVER_URI_PREFIX + getRequest().getServerName()
					+ getRequest().getContextPath();
		else
			return SERVER_URI_PREFIX + getRequest().getServerName() + PORT_SEPARATOR
					+ getRequest().getServerPort()
					+ getRequest().getContextPath();
	}

    /**
     * ActionTime DEBUG 模式下使用
     * @param reg
     */
    public void setActionTimeRequest(HttpServletRequest reg){
           debugRequest=reg;
    }
    


}
